﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Net;
//using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Documents;
//using System.Windows.Input;
//using System.Windows.Media;
//using System.Windows.Media.Animation;
//using System.Windows.Shapes;
//using Microsoft.Phone.Controls;
//using System.IO.IsolatedStorage;


//namespace GeoFinderWP7
//{
//  public partial class MainPage : PhoneApplicationPage
//  {
//    internal const string ServerAddress = "http://localhost:1272";

//    private const string ChannelName = "GeoFinder.Service.GeoFinder";
//    private const string ServiceName = "GeoFinder";

//    private static readonly Uri[] AllowedDomains =
//        {
//            new Uri(ServerAddress)
//        };

//    #region Fields

//    private readonly IsolatedStorageSettings Settings = IsolatedStorageSettings.ApplicationSettings;

//    #endregion

//    #region Properties

//    public string UserName { get { return textBox1.Text; } }

//    #endregion

//    public event EventHandler<LoginEventArgs> Login;
//    public PushContext pushContext;
//    // Constructor
//    public MainPage()
//    {
//      DataContext = this;

//      InitializeComponent();
//      pushContext = new PushContext(ChannelName, ServiceName, AllowedDomains, Dispatcher);


//      Login += userLoginView_Login;
//      Loaded += UserLoginView_Loaded;
//    }

//    private void UserLoginView_Loaded(object sender, RoutedEventArgs e)
//    {
//      string userName;
//      if (Settings.TryGetValue("LoginPage.UserName", out userName))
//      {
//        App.me = userName;
//        textBox1.IsEnabled = false;
//        textBox1.Text = userName;
//        button1.Visibility = Visibility.Collapsed;
//        progressBar1.Visibility = Visibility.Visible;
//        InternalLogin();
//      }
//    }
//    private void button1_Click(object sender, RoutedEventArgs e)
//    {
//      if (!(textBox1.Text == ""))
//      {
//        textBox1.IsEnabled = false;
//        Settings["LoginPage.UserName"] = UserName;
//        App.me = UserName;
//        button1.Visibility = Visibility.Collapsed;
//        progressBar1.Visibility = Visibility.Visible;
//        InternalLogin();
//      }
//      else
//        (new Exception("Insert a name first")).Show();
//    }
//    private void OnLogin(LoginEventArgs args)
//    {
//      if (Login != null)
//      {
//        Login(this, args);
//      }
//    }
//    private void InternalLogin()
//    {
//      var pushContext = PushContext.Current;
//      pushContext.Connect(c => RegisterClient(c.ChannelUri));
//    }
//    private void RegisterClient(Uri channelUri)
//    {
//      // Register the URI with 3rd party web service.
//      try
//      {
//        var pushService = new service.GeoFinderClient();
//        pushService.SubscribeCompleted += (s, e) =>
//        {
//          pushService.CloseAsync();
//          Completed(e.Error);
//        };

//        pushService.SubscribeAsync(UserName, channelUri.ToString(), 1000015);
//      }
//      catch (Exception ex)
//      {
//        Completed(ex);
//      }
//    }
//    private void Completed(Exception ex)
//    {
//      Dispatcher.BeginInvoke(() => OnLogin(new LoginEventArgs(ex)));
//    }

//    private void userLoginView_Login(object sender, LoginEventArgs e)
//    {
//      if (e.Exception != null)
//      {
//        button1.Visibility = Visibility.Visible;
//        progressBar1.Visibility = Visibility.Collapsed;
//        textBox1.IsEnabled = true;
//        e.Exception.Show("Login");
//        return;
//      }
//      //NavigationService.Navigated += new System.Windows.Navigation.NavigatedEventHandler(NavigationService_Navigated);
//      //NavigationService.Navigate(new Uri("/ClientPage.xaml", UriKind.Relative));
//      Application.Current.RootVisual = new ClientPage(pushContext);
//    }

//    void NavigationService_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
//    {
//    }


//  }
//  public class LoginEventArgs : EventArgs
//  {
//    public Exception Exception { get; private set; }

//    public LoginEventArgs(Exception exception = null)
//    {
//      Exception = exception;
//    }
//  }
//  public static class ExceptionExtensions
//  {
//    public static void Show(this Exception ex, string title = "Error")
//    {
//      MessageBox.Show(ex.Message, title, MessageBoxButton.OK);
//    }
//  }
//}